#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>

#include "sm_sem.h"

#warning Benjamin Voir template
 void FaireSemop(int semid, int ValOp)
 {
	 struct sembuf oper;
	 int ret;
	 
	 oper.sem_num = 0;
	 oper.sem_op = ValOp;
	 oper.sem_flg = 0;
	 ret = semop(semid, &oper, 1);
	 
	 if (ret == -1)
	 {
		 perror("\nSEMAPHORE : Problème d'opération\n\n");
	 }
 }



 int initSemaphore(char *fic, char code)
 {
	 key_t clesem;
	 int semid;

	 clesem = ftok(fic, code);
	 
	 if (clesem == -1)
	 {
		 perror("SEMAPHORE : probleme de creation de cle\n\n");
		 exit(0);
	 }

	 semid = semget(clesem, 1 ,IPC_CREAT | 0666);
	 
	 if (semid == -1)
	 {
		 perror("SEMAPHORE : probleme d'ouverture du semaphore\n\n");
		 exit(0);
	 }
	 
	 return semid;
 }
